package com.esri.core.map;

import com.esri.core.geometry.Geometry;
import com.esri.core.geometry.SpatialReference;
import com.esri.core.internal.util.d;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;

/* loaded from: classes.dex */
public class FeatureSet implements Serializable {
    private static final long serialVersionUID = 1;
    List<Graphic> a;
    List<Field> b;
    Integer[] c;
    String d;
    SpatialReference e;
    Geometry.Type f;
    String g;
    transient Map<String, Object> h;
    boolean i;

    public Integer[] getObjectIds() {
        Object obj;
        if (this.c == null && this.d != null && this.a != null) {
            ArrayList arrayList = new ArrayList();
            for (Graphic graphic : this.a) {
                Map<String, Object> attributes = graphic.getAttributes();
                if (attributes != null && (obj = attributes.get(this.d)) != null && (obj instanceof Integer)) {
                    arrayList.add((Integer) obj);
                }
            }
            this.c = (Integer[]) arrayList.toArray(new Integer[0]);
        }
        return this.c;
    }

    public String getObjectIdFieldName() {
        return this.d;
    }

    public boolean isExceededTransferLimit() {
        return this.i;
    }

    public Graphic[] getGraphics() {
        List<Graphic> list = this.a;
        if (list == null) {
            return null;
        }
        return (Graphic[]) list.toArray(new Graphic[0]);
    }

    public void setGraphics(Graphic[] graphicArr) {
        this.a = Arrays.asList(graphicArr);
    }

    public SpatialReference getSpatialReference() {
        return this.e;
    }

    public void setSpatialReference(SpatialReference spatialReference) {
        this.e = spatialReference;
    }

    public Geometry.Type getGeometryType() {
        return this.f;
    }

    public String getDisplayFieldName() {
        return this.g;
    }

    public Map<String, Object> getFieldAliases() {
        return this.h;
    }

    public List<Field> getFields() {
        return this.b;
    }

    public void setFields(List<Field> list) {
        if (list != null && this.b == null) {
            a(this, list);
        }
        this.b = list;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void a(Geometry.Type type) {
        this.f = type;
    }

    public static FeatureSet fromJson(JsonParser jsonParser, int i) throws Exception {
        return fromJson(jsonParser, false, i);
    }

    public static FeatureSet fromJson(JsonParser jsonParser) throws Exception {
        return fromJson(jsonParser, false, -1);
    }

    public static FeatureSet fromJson(JsonParser jsonParser, boolean z) throws Exception {
        return fromJson(jsonParser, z, -1);
    }

    public static FeatureSet fromJson(JsonParser jsonParser, boolean z, int i) throws Exception {
        FeatureSet featureSet = new FeatureSet();
        if (d.c(jsonParser)) {
            while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
                String currentName = jsonParser.getCurrentName();
                jsonParser.nextToken();
                if ("spatialReference".equals(currentName)) {
                    featureSet.e = SpatialReference.fromJson(jsonParser);
                } else if ("exceededTransferLimit".equals(currentName)) {
                    featureSet.i = jsonParser.getBooleanValue();
                } else if ("geometryType".equals(currentName)) {
                    featureSet.f = d.b(jsonParser.getText());
                } else if ("displayFieldName".equals(currentName)) {
                    featureSet.g = jsonParser.getText();
                } else if ("fieldAliases".equals(currentName)) {
                    HashMap hashMap = new HashMap();
                    featureSet.h = hashMap;
                    d.a(jsonParser, hashMap);
                } else if ("features".equals(currentName)) {
                    if (jsonParser.getCurrentToken() == JsonToken.START_ARRAY) {
                        featureSet.a = new ArrayList();
                        while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                            if (i < 0 || featureSet.a.size() < i) {
                                if (z) {
                                    featureSet.a.add(Graphic.fromJson(jsonParser));
                                } else {
                                    featureSet.a.add(d.j(jsonParser));
                                }
                            }
                        }
                    }
                } else if ("fields".equals(currentName)) {
                    if (jsonParser.getCurrentToken() == JsonToken.START_ARRAY) {
                        featureSet.b = new ArrayList();
                        while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                            Field fromJson = Field.fromJson(jsonParser);
                            featureSet.b.add(fromJson);
                            if (80 == fromJson.getFieldType()) {
                                featureSet.d = fromJson.getName();
                            }
                        }
                    }
                } else if ("objectIdFieldName".equals(currentName)) {
                    featureSet.d = jsonParser.getText();
                } else if ("objectIds".equalsIgnoreCase(currentName)) {
                    if (jsonParser.getCurrentToken() == JsonToken.START_ARRAY) {
                        ArrayList arrayList = new ArrayList();
                        while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                            arrayList.add(new Integer(jsonParser.getIntValue()));
                        }
                        featureSet.c = (Integer[]) arrayList.toArray(new Integer[0]);
                    }
                } else {
                    jsonParser.skipChildren();
                }
            }
            if (featureSet.h == null && featureSet.b != null) {
                featureSet.h = new HashMap();
                for (Field field : featureSet.b) {
                    featureSet.h.put(field.getName(), field.getAlias());
                }
            }
            a(featureSet, featureSet.getFields());
            return featureSet;
        }
        return null;
    }

    private static void a(FeatureSet featureSet, List<Field> list) {
        if (featureSet == null || list == null || featureSet.getGraphics() == null || featureSet.getGraphics().length < 1) {
            return;
        }
        for (Field field : list) {
            int i = 0;
            if (field.getFieldType() == 30) {
                String name = field.getName();
                Graphic[] graphics = featureSet.getGraphics();
                int length = graphics.length;
                while (i < length) {
                    Graphic graphic = graphics[i];
                    Object attributeValue = graphic.getAttributeValue(name);
                    if (attributeValue != null && !(attributeValue instanceof Double)) {
                        graphic.setAttributeValue(name, new Double(attributeValue.toString()));
                    }
                    i++;
                }
            } else if (field.getFieldType() == 50) {
                String name2 = field.getName();
                Graphic[] graphics2 = featureSet.getGraphics();
                int length2 = graphics2.length;
                while (i < length2) {
                    Graphic graphic2 = graphics2[i];
                    Object attributeValue2 = graphic2.getAttributeValue(name2);
                    if (attributeValue2 != null && (attributeValue2 instanceof Integer)) {
                        graphic2.setAttributeValue(name2, new Long(attributeValue2.toString()));
                    }
                    i++;
                }
            }
        }
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("FeatureSet [graphics=");
        List<Graphic> list = this.a;
        sb.append(list != null ? list.subList(0, Math.min(list.size(), 3)) : null);
        sb.append(", fields=");
        List<Field> list2 = this.b;
        sb.append(list2 != null ? list2.subList(0, Math.min(list2.size(), 3)) : null);
        sb.append(", objectIds=");
        Integer[] numArr = this.c;
        sb.append(numArr != null ? Arrays.asList(numArr).subList(0, Math.min(this.c.length, 3)) : null);
        sb.append(", objectIdFieldName=");
        sb.append(this.d);
        sb.append(", spatialReference=");
        sb.append(this.e);
        sb.append(", geometryType=");
        sb.append(this.f);
        sb.append(", displayFieldName=");
        sb.append(this.g);
        sb.append("]");
        return sb.toString();
    }

    public static final String toJson(FeatureSet featureSet) throws Exception {
        StringWriter stringWriter = new StringWriter();
        JsonGenerator a = d.a(stringWriter);
        a.writeStartObject();
        String str = featureSet.d;
        if (str != null) {
            a.writeStringField("objectIdFieldName", str);
        }
        a.writeBooleanField("exceededTransferLimit", featureSet.i);
        if (featureSet.getGeometryType() != null) {
            a.writeStringField("geometryType", d.a(featureSet.getGeometryType()));
        } else {
            List<Graphic> list = featureSet.a;
            if (list != null && list.size() > 0) {
                a.writeStringField("geometryType", d.a(featureSet.a.get(0).getGeometry().getType()));
            }
        }
        SpatialReference spatialReference = featureSet.getSpatialReference();
        if (spatialReference != null) {
            a.writeFieldName("spatialReference");
            a.writeStartObject();
            if (spatialReference.getID() > 0) {
                a.writeNumberField("wkid", spatialReference.getID());
            } else {
                a.writeStringField("wkt", spatialReference.getText());
            }
            a.writeEndObject();
        }
        String str2 = featureSet.g;
        if (str2 != null) {
            a.writeStringField("displayFieldName", str2);
        }
        Map<String, Object> map = featureSet.h;
        if (map != null && map.size() > 0) {
            a.writeFieldName("fieldAliases");
            a.writeRawValue(d.b(featureSet.h));
        }
        List<Field> list2 = featureSet.b;
        if (list2 != null && list2.size() > 0) {
            a.writeArrayFieldStart("fields");
            for (Field field : featureSet.b) {
                a.writeRawValue(Field.toJson(field));
            }
            a.writeEndArray();
        }
        Integer[] numArr = featureSet.c;
        if (numArr != null && numArr.length > 0) {
            a.writeArrayFieldStart("objectIds");
            for (Integer num : featureSet.c) {
                a.writeNumber(num.intValue());
            }
            a.writeEndArray();
        }
        List<Graphic> list3 = featureSet.a;
        if (list3 != null && list3.size() > 0) {
            a.writeArrayFieldStart("features");
            for (Graphic graphic : featureSet.a) {
                a.writeRawValue(d.a(graphic, spatialReference));
            }
            a.writeEndArray();
        }
        a.writeEndObject();
        a.close();
        return stringWriter.toString();
    }

    private void writeObject(ObjectOutputStream objectOutputStream) throws IOException {
        objectOutputStream.defaultWriteObject();
        d.a(objectOutputStream, this.h);
    }

    private void readObject(ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
        objectInputStream.defaultReadObject();
        this.h = d.a(objectInputStream);
    }

    public int hashCode() {
        String str = this.g;
        int hashCode = ((str == null ? 0 : str.hashCode()) + 31) * 31;
        List<Graphic> list = this.a;
        int hashCode2 = (hashCode + (list == null ? 0 : list.hashCode())) * 31;
        Geometry.Type type = this.f;
        int hashCode3 = (hashCode2 + (type == null ? 0 : type.hashCode())) * 31;
        SpatialReference spatialReference = this.e;
        return hashCode3 + (spatialReference != null ? spatialReference.hashCode() : 0);
    }

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj != null && getClass() == obj.getClass()) {
            FeatureSet featureSet = (FeatureSet) obj;
            String str = this.g;
            if (str == null) {
                if (featureSet.g != null) {
                    return false;
                }
            } else if (!str.equals(featureSet.g)) {
                return false;
            }
            List<Graphic> list = this.a;
            if (list == null) {
                if (featureSet.a != null) {
                    return false;
                }
            } else if (!list.equals(featureSet.a)) {
                return false;
            }
            Geometry.Type type = this.f;
            if (type == null) {
                if (featureSet.f != null) {
                    return false;
                }
            } else if (!type.equals(featureSet.f)) {
                return false;
            }
            SpatialReference spatialReference = this.e;
            if (spatialReference == null) {
                if (featureSet.e != null) {
                    return false;
                }
            } else if (!spatialReference.equals(featureSet.e)) {
                return false;
            }
            return true;
        }
        return false;
    }
}
